package edu.umn.actvs;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import edu.umn.ftpmaster.ftpClient;

public class UserEventActivity extends Activity{
	
	private Button sync;
	private List<CheckBox> chks = new ArrayList<CheckBox>();
	private List<event>myevents = new ArrayList<event>();
	private EventDBCreator db;
	
	
    private LinearLayout.LayoutParams LP_FF = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
    private LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
    private LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		db = new EventDBCreator(this);
		List<String>events = db.getAllEvents();
		
		Log.w("db", Integer.toString(events.size()));
		final LinearLayout layout2 = new LinearLayout(this);
		layout2.setOrientation(LinearLayout.VERTICAL);
		
		for(Iterator<String>it=events.iterator();it.hasNext();)
		{
			String eName = it.next();
			List<String>eInfo = db.getEventInfo(eName);
			event e=new event();
			e.EventName=eName;
			e.IP = "192.168.0.7";
			e.port=Integer.valueOf(eInfo.get(0));
			e.username=eInfo.get(1);
			e.password=eInfo.get(2);
			
//			Log.w("username", eInfo.get(0)+","+eInfo.get(1)+","+eInfo.get(2));
			
			myevents.add(e);
			CheckBox chk=new CheckBox(this);
			chk.setText(e.EventName);
			chk.setLayoutParams(LP_FW);
			chks.add(chk);

			layout2.addView(chk, LP_FW);
		}
		
		
		
		sync = new Button(this);
		sync.setLayoutParams(LP_WW);

		sync.setText("Sync");
		
		layout2.addView(sync);
		setContentView(layout2);
		
		sync.setOnClickListener(new OnClickListener(){
			@Override
				public void onClick(View arg0) {
					for(Iterator<CheckBox> it = chks.iterator();it.hasNext();){
						CheckBox currentChk = it.next();
						
						if(currentChk.isChecked()){
							//TODO  add event handler
							event e=new event();
							e.EventName=currentChk.getText().toString();
							for(Iterator<event> it1 = myevents.iterator();it1.hasNext();){
								event cur = it1.next();
								if(cur.EventName.equals(e.EventName)){
									e=cur;
									break;
								}
							}
							
//							Log.w("info",e.EventName+e.password+e.username+e.port+e.IP);
							
							new synchronize(e).start();
						}
						
					}
					
					//this is just for test purpose
/*					event e = new event();
					e.EventName="sharetest";
					e.IP="192.168.0.107";
					e.port = 5001;
					e.username="test";
					e.password="test";
					new synchronize(e).start();
*/					//////////////////////////
				} 
			});
	}
	private class event{
		public String EventName;
		public String IP;
		public int port;
		public String username;
		public String password;
		
	}
	private class synchronize extends Thread{
		
		private ftpClient ftpclient; 
		private event ev;
		
		public synchronize(event e){
			super("sync");
			ev = e;
			ftpclient = new ftpClient(true);
			
		}
		
		public List<String> filesToUpload(List<String>remoteFiles, List<String>localFiles){
			List<String> toUp = new ArrayList<String>();
			for(Iterator<String>it=localFiles.iterator();it.hasNext();){
				String cur = it.next();
				if(!remoteFiles.contains(cur)){
					toUp.add(cur);
				}
			}
			return toUp;
		}
		public List<String> filesToDownload(List<String>remoteFiles, List<String>localFiles){
			List<String> toDown = new ArrayList<String>();
			for(Iterator<String>it=remoteFiles.iterator();it.hasNext();){
				String cur = it.next();
				if(!localFiles.contains(cur)){
					toDown.add(cur);
				}
			}
			return toDown;
		}
		
		
		public void run(){
			
			Log.w("sync", "i am in sync");
			
			ftpclient.connect(ev.IP,ev.port);
			ftpclient.login(ev.username, ev.password);
			try {
				String [] available = ftpclient.list();
				
				Log.w("sync", "remote"+Integer.toString(available.length));
				List<String>remoteFiles = new ArrayList<String>();
				for(int i=0;i<available.length;i++){
					remoteFiles.add(available[i]);
				}
				String path = "/sdcard/"+ev.EventName+"/";
				Log.w("sync","name:"+path);
				File f = new File(path);
		    	File[] files = f.listFiles();
		    	
		    	Log.w("sync","length:"+f.isDirectory());
		    	List<String>localFiles = new ArrayList<String>();
		    	for(int i=0;i<files.length;i++){
		    		if(files[i].isFile())
		    			localFiles.add(files[i].getName());
		    	}
		    	Log.w("sync", "local"+Integer.toString(files.length));
		    	List <String> toUpload = filesToUpload(remoteFiles,localFiles);
		    	List <String> toDownload=filesToDownload(remoteFiles,localFiles);
		    	
		    	//upload files to the ftp server
				for(Iterator<String>it=toUpload.iterator();it.hasNext();){
					String cur = it.next();
					ftpclient.send(path+cur, cur);	
				}
				//download files to the ftp server
				for(Iterator<String>it=toDownload.iterator();it.hasNext();){
					String cur = it.next();
					ftpclient.recv(path+cur, cur);	
				}
				
				ftpclient.disconnect();
		    	
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
